package com.example.Sao.MyActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import com.example.Sao.Custom.RxTimer;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class LoginActivity extends MyBaseActivity {
    //定时器
    private RxTimer rxTimer;
    //动态权限申请
    private RxPermissions rxPermissions;
    //权限列表
    private String permissions[] = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA, Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_NETWORK_STATE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //定时器
        rxTimer = new RxTimer();
        //申请权限
        rxPermissions = new RxPermissions(this);
        //Android6.0后需要动态权限申请 使用Rxjava的方式进行权限申请
        rxPermissions.request(permissions).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean){ //权限请求成功
                    rxTimer.timer(2, new RxTimer.IRxNext() {
                        @Override
                        public void doNext(long number, Disposable disposable) {
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            LoginActivity.this.finish();
                        }
                    });
                }else {//权限请求成失败
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    LoginActivity.this.finish();
                }
            }
        });
    }
}